using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Liberty.Application;
using Liberty.Application.ServiceMemberManagement;
using Liberty.Domain.ServiceMemberManagement;

using Peradigm.Framework.Domain.Parties;
using Peradigm.Framework.Persistence;
using Peradigm.Framework.UI.Web;

namespace Liberty.UI.Service.ServiceMemberManagement.Controls
{

    public partial class ServiceMemberView : AbstractControl, IView
    {
        #region Constants
        private const string
            OID = "Oid",
            DESCRIPTION = "Description";
        private const string
            MSG_SELECT_BRANCH = "Select a service branch",
            MSG_SELECT_RANK = "Select a rank",
            SELECT_VALUE = "0";
        private const string
            DOB_YRS = "17";
        #endregion

        #region Event Handlers
        protected void Page_Load( object sender, EventArgs e )
        {
            if( !this.IsPostBack )
            {
                this.ucCurrentAddressView.Model = this.ServiceMember.CurrentAddress;
                this.ucPermanentAddressView.Model = this.ServiceMember.HomeOfRecord;
            }
        }

        protected void ckbSameAddress_CheckedChanged( object sender, EventArgs e )
        {
            if( ( (CheckBox)sender ).Checked )
            {
                this.ucCurrentAddressView.UpdateModel();
                this.ucPermanentAddressView.Model = this.ucCurrentAddressView.Model;
            }
            else
            {
                this.ucPermanentAddressView.Model = new Liberty.Domain.Kernel.Address();
            }
        }

        protected void ddlBranch_SelectedIndexChanged( object sender, EventArgs e )
        {
            Branch selectedBranch = this.ServiceMemberController.BranchWith( long.Parse( this.ddlBranch.SelectedValue ) );
            this.BindRanksFor( selectedBranch );
        }
        #endregion

        #region Methods
        public void SaveModel()
        {
            this.UpdateModel();
            this.ServiceMember = this.ServiceMemberController.SaveServiceMember( this.ServiceMember );
        }

        public override void UpdateView()
        {
            if ( !this.IsAsynchPostback )
            {
                ServiceMember serviceMember = this.ServiceMember;

                this.txtDateOfBirth.Attributes.Add( "onchange", "formattedDate('" + this.txtDateOfBirth.ClientID + "');" );

                this.txtFirstName.Text = serviceMember.FirstName;
                this.txtLastName.Text = serviceMember.LastName;
                this.txtDateOfBirth.Text = this.DateOfBirthFrom( serviceMember );
                this.txtUnit.Text = serviceMember.UnitAssignment.Name;
                this.txtPrimaryPhoneNumber.Text = serviceMember.PrimaryPhoneNumber.AsUnformattedString();
                this.txtSecondaryPhoneNumber.Text = serviceMember.SecondaryPhoneNumber.AsUnformattedString();
                this.BindBranches();
                this.BindRanksFor( this.ServiceMember.Branch );
                this.rvDateOfBirth.MaximumValue = DateTime.Now.AddYears( -( System.Convert.ToInt32( DOB_YRS ) ) ).ToString( "MM/dd/yyyy" );
                
                if((serviceMember.CurrentAddress.DisplayString != "") &&(this.ServiceMember.CurrentAddress.DisplayString == this.ServiceMember.HomeOfRecord.DisplayString))
                    this.ckbSameAddress.Checked = true;
            }
        }

        public override void UpdateModel()
        {
            this.ucCurrentAddressView.UpdateModel();
            this.ucPermanentAddressView.UpdateModel();

            ServiceMember serviceMember = this.ServiceMember;

            serviceMember.FirstName = this.txtFirstName.Text;
            serviceMember.LastName = this.txtLastName.Text;
            serviceMember.UnitAssignment.Name = this.txtUnit.Text;
            serviceMember.BornOn = DateTime.Parse( this.txtDateOfBirth.Text );

            serviceMember.CurrentAddress = (Liberty.Domain.Kernel.Address)this.ucCurrentAddressView.Model;
            serviceMember.PrimaryPhoneNumber = new UnvalidatedPhoneNumber( this.txtPrimaryPhoneNumber.Text );

            serviceMember.HomeOfRecord = (Liberty.Domain.Kernel.Address)this.ucPermanentAddressView.Model;
            serviceMember.SecondaryPhoneNumber = new UnvalidatedPhoneNumber( this.txtSecondaryPhoneNumber.Text );
            serviceMember.Rank = this.RankWith( long.Parse( this.ddlRank.SelectedValue.ToString() ) );

            this.Model = serviceMember;
        }
        #endregion

        #region Properties
        public override object Model
        {
            get
            {
                if( base.Model == null )
                {
                    base.Model = this.Session["ServiceMemeberEditView.Model"];
                }
                return base.Model;
            }
            set
            {
                base.Model = value;
                this.Session["ServiceMemeberEditView.Model"] = base.Model;
            }
        }
        #endregion

        #region Private Methods
        private void BindBranches()
        {
            IList branches = this.ServiceMemberController.AllBranches();
            this.BindList( this.ddlBranch, branches, this.ServiceMember.Branch );
            this.ddlBranch.Items.Insert( 0, new ListItem( MSG_SELECT_BRANCH, SELECT_VALUE ) );
        }

        private void BindRanksFor( Branch branch )
        {
            IList ranks = this.ServiceMemberController.RanksFor( branch );
            this.BindList( this.ddlRank, ranks, this.ServiceMember.Rank );
            this.ddlRank.Items.Insert( 0, new ListItem( MSG_SELECT_RANK, SELECT_VALUE ) );
        }

        private void BindList( DropDownList control, IEnumerable items, ReferenceValue model )
        {
            control.Items.Clear();

            control.DataSource = items;
            control.DataValueField = OID;
            control.DataTextField = DESCRIPTION;
            control.DataBind();

            control.ClearSelection();
            if( model != null )
            {
                ListItem selectableItem = control.Items.FindByValue( model.Oid.ToString() );
                if( selectableItem != null )
                {
                    selectableItem.Selected = true;
                }
            }
        }

        private string RankFrom( ServiceMember serviceMember )
        {
            if( serviceMember.Rank != null )
            {
                return serviceMember.Rank.DisplayString;
            }
            return String.Empty;
        }

        private string BranchFrom( ServiceMember serviceMember )
        {
            if( serviceMember.Branch != null )
            {
                return serviceMember.Branch.DisplayString;
            }
            return String.Empty;
        }

        private string DateOfBirthFrom( ServiceMember serviceMember )
        {
            if( serviceMember.BornOn != null )
            {
                return serviceMember.BornOn.Value.ToString( "MM/dd/yyyy" );
            }
            return String.Empty;
        }

        private Rank RankWith( long rankID )
        {
            Rank rank = null;

            rank = this.ServiceMemberController.RankWith( rankID );

            return rank;
        }
        #endregion

        #region Private Properties
        private ServiceMember ServiceMember
        {
            get
            {
                return (ServiceMember)this.Model;
            }
            set
            {
                this.Model = value;
            }
        }

        private ServiceMemberController ServiceMemberController
        {
            get
            {
                return this.i_ServiceMemberController;
            }
        }

        private bool IsAsynchPostback
        {
            get
            {
                return ScriptManager.GetCurrent( this.Page ).IsInAsyncPostBack;
            }
        }
        #endregion

        #region Construction and Finalization
        #endregion

        #region Data Elements
        private ServiceMemberController i_ServiceMemberController = new ServiceMemberController();
        #endregion
        
}
}
